package cn.nange.cmfz.controller;

import cn.afterturn.easypoi.excel.ExcelExportUtil;
import cn.afterturn.easypoi.excel.ExcelImportUtil;
import cn.afterturn.easypoi.excel.entity.ExportParams;
import cn.afterturn.easypoi.excel.entity.ImportParams;
import cn.nange.cmfz.annotation.LogAnnotation;
import cn.nange.cmfz.entity.Guru;
import cn.nange.cmfz.service.GuruService;
import cn.nange.cmfz.util.ExcelUtils;
import com.github.pagehelper.PageInfo;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.net.URLEncoder;
import java.util.*;

@Controller
@RequestMapping("guru")
public class GuruController {

    @Autowired
    private GuruService guruService;

    @RequestMapping("add")
    @ResponseBody
    public Map add(Guru g) {
        HashMap map = new HashMap();
        try {
            guruService.add(g);
            map.put("isAdd", true);
        } catch (Exception e) {
            e.printStackTrace();
            map.put("isAdd", false);
        }
        return map;
    }

    @ResponseBody
    @RequestMapping("showOne")
    public Guru showOne(Integer id) {
        System.out.println(id);
        Guru guru = guruService.showOne(id);
        System.out.println("guru = " + guru);
        return guru;
    }

    @ResponseBody
    @RequestMapping("showAll")
    public List<Guru> showAll() {
        List<Guru> gurus = guruService.showAll();
        System.out.println("gurus = " + gurus);
        return gurus;
    }

    @ResponseBody
    @RequestMapping("update")
    public Map update(Guru g) {
        HashMap map = new HashMap();
        try {
            guruService.update(g);
            map.put("isUpdate", true);

        } catch (Exception e) {

            e.printStackTrace();

            map.put("isUpdate", false);
        }
        System.out.println("update:" + g);
        return map;
    }

    @ResponseBody
    @RequestMapping("delete")
    public Map delete(int[] id) {
        HashMap map = new HashMap();
        try {
            for (int i = 0; i < id.length; i++) {
                guruService.delete(id[i]);
                System.out.println(id[i]);
                map.put("isDelete", true);
            }

        } catch (Exception e) {
            e.printStackTrace();
            map.put("isDelete", false);
        }
        return map;
    }

    @ResponseBody
    @RequestMapping("showPage")
    public Map showPage(Integer page, Integer limit) {
        PageInfo<Guru> show = guruService.showAllPage(page, limit);
        HashMap map = new HashMap();
        map.put("code", 0);
        map.put("msg", "");
        map.put("count", show.getTotal());
        map.put("data", show.getList());
        return map;
    }

    @ResponseBody
    @RequestMapping("upload")
    public Map upload(MultipartFile file) throws IOException {
        String s = UUID.randomUUID().toString() + "-" + file.getOriginalFilename();
//        file.transferTo(new File("D://static",s));
        file.transferTo(new File("/Users/nange/static/cmfz", s));
        HashMap map = new HashMap();
        map.put("code", 0);
        map.put("photo", s);
        System.out.println("upload:" + map);
        return map;
    }

    @RequestMapping("export")
    @LogAnnotation(content = "导出上师数据", type = "数据导出")
    public void exporting(HttpServletResponse resp) throws IOException {

        List<Guru> gurus = guruService.showAll();

        Workbook workbook = ExcelExportUtil.exportExcel(new ExportParams("上师数据", "上师"), Guru.class, gurus);
        /**
         * 参数1 导出参数对象 标题和表名定义 * 参数2 被导出的实体类的类对象
         * 参数3 所有的上师的数据
         */

        resp.setCharacterEncoding("UTF-8");
        resp.setHeader("content-Type", "application/vnd.ms-excel");
        resp.setHeader("Content-Disposition",
                "attachment;filename=" + URLEncoder.encode("全部上师数据.xls", "UTF-8"));
//        Workbook workbook = ExcelExportUtil.exportExcel(new ExportParams("全部上师数据", "上师"), Guru.class, gurus);

        workbook.write(resp.getOutputStream());

    }

    @RequestMapping("import")
    @ResponseBody
    @LogAnnotation(content = "导入上师数据", type = "数据导入")
    public Map importing(MultipartFile file) throws Exception {

        HashMap map = new HashMap();
        try {
            InputStream inputStream = file.getInputStream();
            ImportParams importParams = new ImportParams();
            importParams.setTitleRows(1);
            importParams.setHeadRows(1);

//            FileInputStream fis = new FileInputStream("/Users/nange/static/cmfz/xls/上师数据.xls");
//            List<Guru> gurus = ExcelUtils.importExcel(realPath, 1,1, Guru.class);

            List<Guru> gurus = ExcelImportUtil.importExcel(inputStream, Guru.class, importParams);

            for (Guru g : gurus) {
                System.out.println(g);
                guruService.add(g);
            }

            map.put("Import",true);
        }catch (Exception e){
            map.put("Import",false);
        }
        return map;
    }
}
